﻿using System;
using System.Data;

namespace Syno.Core.Output.DataLoader
{
    public class SqlDataLoader : IDataLoader
    {
        private IDatabase _connection;
        public string[] DataRequired;
        public string Sql { get; set; }
        protected INVelocityTemplates Templater;


        
        public SqlDataLoader() : this(null)
        {
        }

        public SqlDataLoader(IDatabase connection)
        {
            
            _connection = connection;
        }

        public void SetTemplater(INVelocityTemplates dataHolders)
        {
            Templater = dataHolders;
        }


        public IDatabase Connection
        {
            get { return _connection; }
            set { _connection = value; }
        }

        public DataTable ReadData()
        {
            if (_connection == null) throw new Exception("Database connection not found");
            using (var transaction = _connection.GetTransaction()){
                return _connection.ExecuteQuery(Templater.ToString(Sql), transaction);
            }
        }
    }
}